// pages/createIntegralConvertProduct/createIntegralConvertProduct.js
let app = getApp()
Page({
    
  /**
   * 页面的初始数据
   */
  data: {
      levelArray: [
          {
            name: '全部会员',
            level: 0
          }
      ],
      levelIndex: 0,
      info: {
          couponGroupId: '',
          name: '',
          points: '',
          levelRestrict: '0',
          images: '',
          lowerBound: '',
          upperBound: ''
      },
      couponPoolIds: [],
      images: []
  },
  navImage(){
    wx.navigateTo({
        url: `/pages/uploadProductImage/uploadProductImage?type=pointMallImages&images=${JSON.stringify(this.data.images)}`
    })
  },
  levelPickerChange(e){
      this.data.info.levelRestrict = this.data.levelArray[e.detail.value].level
      this.setData({
          levelIndex: e.detail.value,
          info: this.data.info
      })
  },
  create(){
      let info = this.data.info
      info.lowerBound = new Date().toLocaleString().split(' ')[0].split('/').join('-')
      if (Object.values(info).filter(val => String(val).trim() === '').length) {
          wx.showModal({
              title: '提示',
              content: '尚有信息未填写',
              showCancel: false
          })
          return
      }
      app.globalData.post({
          url: '/pointMall/create',
          data: info,
          success: () => {
              wx.navigateBack({})
          }
      })
  },
    timePickerChange(e){
        let value = e.detail.value
        this.data.info.upperBound = value
        this.setData({
            info: this.data.info
        })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.globalData.post({
        url: '/level/get',
        data: {
            shopId: app.globalData.shopId
        },
        success: (data) => {
            let levelInfo = data.data.levelDetail ? data.data.levelDetail : []

            let convert = levelInfo.map(val => {
                return {
                    name: val.name,
                    level: val.level
                }
            })

            this.setData({
                levelArray: [...this.data.levelArray, ...convert]  
            })
        }
    })
  },
  navTicket() {
      wx.navigateTo({
          url: `/pages/ticketsChoose/ticketsChoose?type=choose&couponPoolIds=${JSON.stringify(this.data.couponPoolIds)}`,
      })
  },
  setCouponPool({ couponPoolIds, couponPoolId }) {
      this.data.info.couponGroupId = couponPoolId
      this.setData({
          couponPoolIds,
          info: this.data.info
      })
  },
  iN(e){
    this.data.info.name = e.detail.value
    this.setData({
        info: this.data.info
    })
  },
  iP(e){
      this.data.info.points = e.detail.value
      this.setData({
          info: this.data.info
      })
  },
    setImages(images){
        this.data.images = images
        this.data.info.images = JSON.stringify(images)
        this.setData({
            images: this.data.images,
            info: this.data.info
        })
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})